package controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.apache.myfaces.custom.fileupload.UploadedFile;

import model.NewsRepository;
import model.UserRepository;
import myBeans.NewsInfo;
import myBeans.UserInfo;

/**
 * Servlet implementation class HotNewsUpdateServlet
 */
@WebServlet("/HotNewsUpdate")
@MultipartConfig
public class HotNewsUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 //private final static Logger LOGGER = Logger.getLogger(FileUploadServlet.class.getCanonicalName());
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HotNewsUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}
	
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		NewsInfo news = new NewsInfo();
		
		try {
		    List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
	        for (FileItem item : items) {
	            if (item.isFormField()) {
	                // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
	                String fieldName = item.getFieldName();
	                String fieldValue = item.getString();
	                
	                if(fieldName.equalsIgnoreCase("title")){
	                	news.setNewsTitle(fieldValue);
	                }
	                
	                if(fieldName.equalsIgnoreCase("detail")){
	                	news.setNewsDetail(fieldValue);
	                }
	                
	            } else {
	                news.setNewsAttachFile(item);
	            }
	        }
	    } catch (FileUploadException e) {
	        throw new ServletException("Cannot parse multipart request.", e);
	    }
	    
		
		try {
			NewsRepository newRepo = new NewsRepository();
			
			boolean addSuccess = newRepo.AddNews(news);
			
			if(addSuccess){
				response.sendRedirect("UpdateNews_Success.jsp");
			}
			else{
				response.sendRedirect("UpdateNews_Fail.jsp");
			}
			
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

}
